package org.xshop.web;

import java.math.BigDecimal;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.xshop.core.ExpressmanHelper;
import org.xshop.model.TExpressman;
import org.xshop.model.TExpressmanAccount;
import org.xshop.model.TExpressmanTrade;
import org.xshop.service.ITExpressmanAccountService;
import org.xshop.service.ITExpressmanService;
import org.xshop.service.ITExpressmanTradeService;
import org.xshop.service.ITOrderExpressService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import top.ibase4j.core.base.AppBaseController;
import top.ibase4j.core.support.Pagination;
import top.ibase4j.core.util.DataUtil;
import top.ibase4j.core.util.InstanceUtil;
import top.ibase4j.core.util.WebUtil;

/**
 * <p>
 * 骑手收入明细  前端控制器
 * </p>
 *
 * @author ShenHuaJie
 * @since 2018-05-30
 */
@Controller
@RequestMapping("/app/expressman/")
@Api(tags = "ExpressmanTrade", value = "骑手收入明细接口", description = "骑手收入明细接口")
public class TExpressmanTradeController extends AppBaseController<TExpressmanTrade, ITExpressmanTradeService> {
    @Autowired
    private ITExpressmanService expressmanService;
    @Autowired
    private ITExpressmanAccountService expressmanAccountService;
    @Autowired
    private ITOrderExpressService orderExpressService;

    @ApiOperation(value = "查询骑手收支明细", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping(value = "tradeList.api", method = {RequestMethod.GET, RequestMethod.POST})
    public Object query(HttpServletRequest request) {
        Map<String, Object> params = WebUtil.getParameter(request);
        Long expressmanId = ExpressmanHelper.getCurrent(request);
        params.put("expressmanId", expressmanId);
        Pagination<TExpressmanTrade> pagination = service.query(params);
        Pagination<TExpressmanAccount> accounts = expressmanAccountService.query(params);
        logger.info("pagination=====>" + pagination);
        TExpressman expressman = expressmanService.queryById(expressmanId);
        ModelMap modelMap = new ModelMap();
        BigDecimal zero = new BigDecimal("0");
        modelMap.put("accounts", accounts);
        modelMap.put("money", DataUtil.ifNull(expressman.getMoney(), zero));
        modelMap.put("scoure", DataUtil.ifNull(expressman.getScoure(), 0));
        params = InstanceUtil.newHashMap();
        params.put("expressmanId", expressmanId);
        params.put("tradeType", "1");
        modelMap.put("inMoney", DataUtil.ifNull(service.getSumMoney(params), zero));
        params.put("tradeType", "2");
        modelMap.put("outMoney", DataUtil.ifNull(service.getSumMoney(params), zero));
        modelMap.put("expMoney", DataUtil.ifNull(orderExpressService.getExpMoney(params), zero));
        return setSuccessModelMap(modelMap, pagination);
    }

    @PostMapping("trade.api")
    @ApiOperation(value = "提现", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object update(HttpServletRequest request, Long accountId, BigDecimal tradeMoney) {
        TExpressmanTrade param = WebUtil.getParameter(request, TExpressmanTrade.class);
        Assert.notNull(param.getAccountId(), "账号ID不能为空.");
        Assert.notNull(param.getTradeMoney(), "提现金额不能为空.");
        param.setExpressmanId(ExpressmanHelper.getCurrent(request));
        param.setClientIP(WebUtil.getHost(request));
        service.trade(param);
        return setSuccessModelMap();
    }
}
